from wsgiref.validate import validator

from django import forms
from django.core import validators

"""
表单的验证
CharField
    min_length
    max_length
    required   这个字段是否为必须
    error_messages
EmailField

FloatField
    min_length
    max_length
IntegerField
    min_length
    max_length
UrlField

验证器
MaxValueValidator
MinValueValidator
MinLengthValidator
MaxLengthValidator
EmailValidator
URLValidator
RegexValidator


提取错误信息

form.errors
form.errors.get_json_data()
form.errors.as_json()
"""


#留言表的表单
class MessageBoardFrom(forms.Form):
    title = forms.CharField(min_length=2, max_length=20,label="标题",
                            error_messages={"min_length": "标题最小长度不能小于2！",
                                            "max_length":"标题最小长度不能超过20！"})
    content = forms.CharField(widget=forms.Textarea(attrs={'rows':4}),label = "内容")
    email = forms.EmailField(label="邮箱")

class RegisterForm(forms.Form):
    telephone = forms.CharField(validators=[validators.RegexValidator(regex=r'1[345678]\d{9}',message="手机号码格式不符合！")])
    pwd1 = forms.CharField(widget=forms.PasswordInput(render_value=False))
    pwd2 = forms.CharField(widget=forms.PasswordInput(render_value=False))
    def clean_telephone(self):
        telephone = self.cleaned_data['telephone']
        if telephone == "1888888888":
            pass
        else:
            pass

    def clean(self):
        clean_data = super.clean()
        pwd1 = clean_data['pwd1']
        pwd2 = clean_data['pwd2']
        if pwd1 != pwd2:
            pass
        else:
            print(form.errors)
            pass